package com.atguigu.tingshu.order.mapper;

import com.atguigu.tingshu.model.order.OrderInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import jakarta.validation.constraints.NotEmpty;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface OrderInfoMapper extends BaseMapper<OrderInfo> {
    @Select("SELECT\n" +
            "COUNT(*)\n" +
            "FROM\n" +
            "order_info oi\n" +
            "INNER JOIN order_detail od ON oi.id = od.order_id\n" +
            "WHERE\n" +
            "oi.user_id = #{userId}\n" +
            "AND oi.item_type = #{itemType}\n" +
            "AND oi.order_status = '0901'\n" +
            "AND od.item_id = #{albumId}")
    Long getItemAlbumAndVipIsPadding(@Param("userId") Long userId, @NotEmpty(message = "付款项目类型不能为空") @Param("itemType") String itemType, @Param("albumId") Long albumId);

    @Select("SELECT\n" +
            "od.item_id\n" +
            "FROM\n" +
            "order_info oi\n" +
            "INNER JOIN order_detail od ON oi.id = od.order_id\n" +
            "WHERE\n" +
            "oi.user_id = #{userId}\n" +
            "AND oi.item_type = #{itemType}\n" +
            "AND oi.order_status = '0901'")
    List<Long> getItemTrackIsPadding(Long userId, @NotEmpty(message = "付款项目类型不能为空") String itemType);

    @Update("UPDATE order_info SET order_status = '0902' WHERE order_no = #{orderNo} AND user_id = #{userId} AND order_status='0901'")
    int updateOrderStatus(String orderNo, Long userId);

    @Update("UPDATE order_info SET order_status = '0903' WHERE order_no = #{orderNo} AND order_status='0901'")
    int cancelOrder(String orderNo);
}
